package wpRelations;

import java.util.ArrayList;

import processing.core.PApplet;

public class BinaryTable {
	PApplet parent;
	ArrayList<String> rowLabels;
	ArrayList<String> coloumnLabels;
	int[][] content;
	String[] rLabels;
	String[] cLabels;
	int[] colSum ;
	
	public BinaryTable(PApplet p5, int[][] content) {
		parent = p5;
		this.content = content;
	}
	
	public BinaryTable(PApplet p5, TsvTable tableContent) {
		parent = p5;
		this.content = stringArrayToIntArray(tableContent.data);
	}
	
	public void setRowLabels(ArrayList<String> theLabels) {
		rowLabels = theLabels;
		if (rowLabels.size() != content.length) System.out.println("Content / Row Label size mismatch!");
		rLabels = new String[rowLabels.size()];
		for (int i = 0; i < rLabels.length; i++) {
			rLabels[i] = rowLabels.get(i);
		}
	}

	public void setColoumnLabels(ArrayList<String> theLabels) {
		coloumnLabels = theLabels;
		if (coloumnLabels.size() != content[0].length) System.out.println("Content / Coloumn Label size mismatch!");
		cLabels = new String[coloumnLabels.size()];
		for (int i = 0; i < cLabels.length; i++) {
			cLabels[i] = coloumnLabels.get(i);
		}
	}
	
	int getInt(int row, int col) {
		return content[row][col];
	}
	
	public void printToConsole() {
		parent.print(" |");
		for (int i = 0; i < content[0].length; i++) {
			try {
				parent.print(cLabels[i] + " |");	
			} catch (Exception e) {
				// TODO: handle exception
			}	
		}
		parent.println();
		for (int i = 0; i < content.length; i++) {
			
			
			
			try {
				parent.print(rLabels[i]);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			for (int j = 0; j < content[0].length; j++) {
				parent.print("| "+content[i][j]);
			}
			parent.println();
		}
		for (int i = 0; i < content[0].length; i++) {
		parent.print(colSum[i] + "| ");
		}
	}
	
	public void sortColoumns() {
		int[] sortedOrder = new int[content[0].length];
		for (int i = 0; i < content[0].length; i++) {
			sortedOrder[i] = i; 
		}
		
		colSum = new int[content[0].length];
		for (int i = 0; i < content[0].length; i++) {
			for (int j = 0; j < content.length; j++) {
				colSum[i] += content[j][i];
			}
		}
		
		int counter = 0;
		for (int i = 0; i < content[0].length; i++) {
			for (int j = 0; j < content[0].length-counter-2; j++) {
				if (colSum[j]<colSum[j+1]) {
					swapLabel(cLabels,j,(j+1));
					swapColoumns(j, (j+1));
					colSum = swapInt(colSum,j,(j+1));
				}
			}
			counter++;
		}
	}
	
	int[] getColoumn (int i) {
		int[] result = new int[content.length];
			for (int j = 0; j < content.length; j++) {
				result[j] = content[j][i];
			}
			return result;
		}
	
	void setColoumn(int i, int[] inputColoumn) {
		for (int j = 0; j < content.length; j++) {
			content[j][i] = inputColoumn[j];
		}
	}
	
	void swapLabel(String[] theLabel, int i, int j) {
		String a = theLabel[i];
		String b = theLabel[j];
		theLabel[j] = a;
		theLabel[i] = b;
	}
	
	int[] swapInt(int[] array, int i, int j) {
		int a = array[i];
		int b = array[j];
		array[j] = a;
		array[i] = b;
		return array;
	}
	
	void swapColoumns(int i, int j) {
		int[] a = getColoumn(i);
		int[] b = getColoumn(j);
		setColoumn(j, a);
		setColoumn(i, b);
	}
	
	int[][] stringArrayToIntArray(String[][] s) {
		int[][] result = new int[s.length][s[0].length];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[0].length; j++) {
				result[i][j] = parent.parseInt(s[i][j]);
			}
		}
		return result;
	}
}
